package com.example.demo2.controller;

import com.example.demo2.entity.*;
import com.example.demo2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class EquipmentController {
    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrdersDetailMapper ordersDetailMapper;
    @Autowired
    SolutionMapper solutionMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    StandardMapper standardMapper;

    @GetMapping("/findOrdersByUID")
    public List<Orders> findOrdersByUID(HttpSession session) {
        Integer uID = (Integer) session.getAttribute("uID");
        List<Orders> orderss = ordersMapper.findOrdersByUID(uID);
        for (Orders orders : orderss) {
            List<OrdersDetail> details = ordersDetailMapper.findOrdersDetailByOId(orders.getoId());
            for (OrdersDetail ordersDetail : details) {
                //解决方案名称
                Solution solution = solutionMapper.findSolutionBySoId(ordersDetail.getSoId());
                if(solution != null) {
                    ordersDetail.setSoName(solution.getSoName());
                }
                //设备名称
                Equipment equipment = equipmentMapper.findEquipmentByeId(ordersDetail.getEId());
                if(equipment != null) {
                    ordersDetail.setEName(equipment.geteName());
                }
                //项目名称、GB
                Project project = projectMapper.findProjectByPjId(ordersDetail.getPjId());
                if(project != null) {
                    ordersDetail.setPjName(project.getPjName());
                    ordersDetail.setPjGB(project.getPjGB());
                    Standard standard = standardMapper.findStandardByStaId(project.getStaId());
                    if(standard != null) {
                        ordersDetail.setStaName(standard.getStaName());
                    }
                }
            }
            orders.setDetails(details);
        }
        return orderss;
    }

    @PutMapping("/deleteOrders")
    public int deleteOrders(int oId) {
        return ordersMapper.deleteOrdersByOID(oId)&ordersDetailMapper.deleteOrdersDetailByOId(oId);
    }
}
